package com.yanqu.road.server.logic.activity.condition;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.task.args.union.UnionActivityTaskArgs;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.activity.UnionActivityTaskMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;

public abstract class BaseUnionActivityTaskCondition extends BaseActivityCondition {

    public BaseUnionActivityTaskCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
        super(activityInfo, activityConditionInfo, baseActivityData);
    }

    /**
     * 修改condition（商会任务、成就）
     */
    public final void applyUnionActivityArgs(UnionActivityTaskArgs args, BaseUnionActivityTaskCondition baseClass) {
        String unionUid = GameUnionActivityGroupMgr.getActivityUnionUid(activityInfo, player);
        if (StringUtils.isNullOrEmpty(unionUid)) {
            return;
        }

        UnionActivityTaskMgr.addUploadData(
                activityInfo.getActivityId(),
                unionUid,
                baseClass.getConditionId(),
                args.getAddValue()
        );
    }

    /**
     * 个人领奖条件
     */
    @Override
    public boolean isCompleted(GamePlayer player) {
        return getValue().compareTo(activityConditionInfo.getParamList().get(3)) >= 0;
    }

    /**
     * 更新操作
     */
    @Override
    protected void player_BoardLevelHandle(Object object) {
        UnionActivityTaskArgs args = (UnionActivityTaskArgs) object;
        if (!isSevenDayCanSet()) {
            return;
        }
        applyUnionActivityArgs(args, this);
    }

    @Override
    public void removeTrigger(GamePlayer player) {
        // 移除掉之后，自己任务完成了，就不加商会次数了
    }

    /**
     * 检查七日活动设置
     */
    public boolean isSevenDayCanSet() {
        BigInteger day = activityConditionInfo.getParamList().get(2);
        if (day.compareTo(BigInteger.ZERO) == 0) {
            return true;
        } else {
            ActivityInfo curActivity = NormalActivityMgr.getActivityInfo(activityInfo.getActivityId());
            if (null == curActivity) {
                curActivity = activityInfo;
            }
            long zeroTime = DateHelper.getZeroTimeStamp(curActivity.getBeginTime() * 1000);
            long canUpdateTime = zeroTime + (day.longValue() - 1) * 24 * 60 * 60 * 1000;
            if (System.currentTimeMillis() >= canUpdateTime) {
                return true;
            }
        }
        return false;
    }

}
